package com.barbeque.mall.config.realm;

import com.barbeque.mall.bean.InfoData;
import com.barbeque.mall.bean.advertise.MarketAd;
import com.barbeque.mall.bean.system.MarketAdmin;
import com.barbeque.mall.bean.system.MarketAdminExample;
import com.barbeque.mall.bean.system.MarketPermission;
import com.barbeque.mall.bean.system.MarketRole;
import com.barbeque.mall.mapper.MarketAdminMapper;
import com.barbeque.mall.mapper.MarketRoleMapper;
import com.barbeque.mall.service.AdminAuthService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author why
 * @since 2022/07/19 17:51
 */
@Component
public class CustomRealm extends AuthorizingRealm {

    //拿钥匙
    @Autowired
    MarketAdminMapper adminMapper;

    @Autowired
    MarketRoleMapper roleMapper;

    @Autowired
    AdminAuthService adminAuthService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //要先获得Principal
        MarketAdmin marketAdmin = (MarketAdmin) principalCollection.getPrimaryPrincipal();
        Integer[] roleIds = marketAdmin.getRoleIds();
        ArrayList<String> perms = new ArrayList<>();
        for (Integer roleId : roleIds) {
            List<MarketPermission> permissionByRoleId = adminAuthService.getPermissionByRoleId(roleId);
            for (MarketPermission marketPermission : permissionByRoleId) {
                String permission = marketPermission.getPermission();
                perms.add(permission);
            }
        }
        List<String> permissions = perms;
//        InfoData infoData = new InfoData();
//        infoData.setPerms(perms);

        //根据用户信息查询出对应的权限列表
        //用mybatis来查询
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        simpleAuthorizationInfo.addStringPermissions(permissions);
        return simpleAuthorizationInfo;
    }


    //认证过程
    //上锁
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //通常根据AuthenticationToken中传入的用户名信息查询出该用户在数据库中的信息
        String username = (String) authenticationToken.getPrincipal();
        MarketAdminExample example = new MarketAdminExample();
        example.createCriteria().andUsernameEqualTo(username);
        List<MarketAdmin> marketAdmins = adminMapper.selectByExample(example);
        if (marketAdmins.size() == 1) {
            //说明数据库中有一条对应的信息
            MarketAdmin marketAdmin = marketAdmins.get(0);
            //构造认证信息时，可以放入你需要的用户信息，而你放入的用户信息，可以作为Principals
            //放入这个信息，是为了取出这个信息
            //第二个参数是credentials,是真实（正确）的密码，会和AuthenticationToken中的password做比较
            return new SimpleAuthenticationInfo(marketAdmin, marketAdmin.getPassword(), getName());
        }
        return null;
    }
}
